package com.reverie.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reverie.common.pojo.CommonPageReq;
import com.reverie.common.pojo.CommonResult;
import com.reverie.entity.Role;
import com.reverie.dto.common.DeleteParam;
import com.reverie.dto.user.RoleSaveParam;
import com.reverie.entity.User;
import com.reverie.service.IRoleService;
import com.reverie.service.IUserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("admin/v1/role")
public class RoleController {

    @Resource
    private IRoleService roleService;
    @Resource
    private IUserService userService;

    @PostMapping("queryPage")
    public CommonResult<Page<Role>> queryPage(@RequestBody CommonPageReq<Role> queryPageParam){
        return CommonResult.data(roleService.queryPage(queryPageParam));
    }

    @PostMapping("queryList")
    public CommonResult<List<Role>> queryList(){
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        Object loginId = StpUtil.getLoginId();
        User user = userService.getById(loginId.toString());
        if (user.getRoleId() != 202401) {
            wrapper.lambda().eq(Role::getCreateUserId,loginId.toString());
        }
        return CommonResult.data(roleService.list(wrapper));
    }

    @PostMapping("save")
    public CommonResult<Boolean> save(@RequestBody RoleSaveParam roleSaveParam){
        return CommonResult.data(roleService.onSave(roleSaveParam));
    }

    @PostMapping("delete")
    public CommonResult<Boolean> delete(@RequestBody DeleteParam deleteParam){
        return CommonResult.data(roleService.removeById(deleteParam.getId()));
    }

}
